
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#include "Geometry.h"

//storage
#include <vector>
using std::vector;

class GeoVertex;

//structure for a mesh's vertex
class meshVertex
{
public:
	CVector3f _position;
	CVector3f _normal;

	meshVertex(const CVector3f& pos, const CVector3f& n):_position(pos), _normal(n){}
};

//this class represent a mesh editable by the user (uses a collection of triangles)
class GeoMesh : public Geometry
{
public:

	//vertex buffer storing mesh's vertices
	vector<meshVertex> _vertexBuffer;

	//index buffer describing triangles
	vector<int> _indexBuffer;

	//buffer of normal(per triangles)
	vector<CVector3f> _normalPerTriangleBuffer;

	//uvmap buffer
	vector<CVector3f> _uvMapBuffer;

private:
	//buffer of pointer to the geovertex
	vector<GeoVertex*> _geoVertexBuffer;

	//axis aligned bounding box
	float _aabb[6];

	//id of a vertex composing the triangle to draw
	int _triangleDrawVertexID;

	//id of the to draw containing _triangleDrawVertexID
	int _triangleDrawTriangleCount;

public:
	//constructor
	GeoMesh();

	//destructor
	virtual ~GeoMesh();

	//determine if the point v is a point of the geometry object
	bool isGeometry(const CVector3f& v)const;

	//pre render the object
	void preRender();

	//render the object
	void Render();

	//post render function
	void postRender();

	//create a cube mesh
	void createCube(int size);

	//create a plan mesh
	void createPlan(int size);

	//set the position of a vertex
	void setVertexPosition(int vertexIndex, const CVector3f& newPos);

	//get the position of a vertex in local coordinate
	CVector3f getVertexPosition(int vertexIndex);

	//calculate the axis aligned bounding box
	void calculateAABB();

	//add a vertex on the line v1;v2
	bool addVertex(int v1, int v2);

	//delete a vertex
	void deleteVertex(int vertexID);

	//merge two vertices
	bool mergeVertices(int v1, int v2);

	//delete all the geovertex of the mesh
	void deleteAllGeoVertex();

	//calculate normals of the triangles containing the point identifying with vertexID
	void calculateTrianglesNormal(int vertexID);

	//update the AABB when the vertex vertexID has been moved
	void updateAABB(int vertexID);

	//calculate the normals per vertex
	void calculateNormalPerVertex();

	//return the number of triangle in the mesh
	int getTrianglesCount()const;

	//get the numver of triangles owning a vertex
	int getTrianglesCount(int vertexID)const;

	//set the value to draw the selected triangle
	void setDrawSpecialTriangle(int vertexID, int triangleCount);

	//get the uv map of a vertex in a specific triangle
	CVector3f getUVMap(int vertexID, int triangleCount)const;

	//set the uv map of a vertex in a specific triangle
	void setUVMap(int vertexID, int triangleCount, const CVector3f& uv);

	//create all the geovertex corresponding to the mesh
	void createAllGeoVertex();

private:

	//draw the selected triangle (if selected)
	void drawSelectedTriangle();
};